package com.fengmi.portal.service;

import cn.hutool.json.JSONUtil;
import com.fengmi.api.cms.CmsApi;
import com.fengmi.api.goods.GoodsApi;
import com.fengmi.cms.Content;
import com.fengmi.goods.vo.MallGoodsAndCatVO;
import com.fengmi.goods.vo.MallGoodsCatVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class PortalServiceImpl implements PortalService {
    @Autowired
    StringRedisTemplate template;
    @Autowired
    CmsApi cmsApi;
    @Autowired
    GoodsApi goodsApi;
  //  string / hash    KEY,key,value    "portal:cms"  categoryid list.string
                                                 //        1            str
                                                 //        2             str2
    //
    @Override
    public List<Content> getAllContentsList(Long categoryId) {
        BoundHashOperations<String, String, String> cms = template.boundHashOps("portal:cms");
        String s = cms.get(categoryId + "");
        if(StringUtils.isEmpty(s)){  //为空redis没有
            //通过feign去cms模块查，查到后返回
            List<Content> allContentsList = cmsApi.getAllContentsList(categoryId);
            //通过Hutool工具包把List对象转化成String
            String cts = JSONUtil.toJsonStr(allContentsList);
            //把字符串放到redis缓存中
            cms.put(categoryId+"",cts);
            return  allContentsList;
        }
        //通过hutool把string 转成list<Content>
        List<Content> contents = JSONUtil.toList(s, Content.class);
        return contents;
    }

    @Override
    public List<MallGoodsCatVO> getAllGoodsCat() {
        BoundValueOperations<String, String> goodscat = template.boundValueOps("portal:goodscat");
        String cats = goodscat.get();
        if(StringUtils.isEmpty(cats)){  //为空
            List<MallGoodsCatVO> allGoodsCat = goodsApi.getAllGoodsCat();
            String s = JSONUtil.toJsonStr(allGoodsCat);
             goodscat.set(s);
             return  allGoodsCat;
        }
        List<MallGoodsCatVO> mallGoodsCatVOS = JSONUtil.toList(cats, MallGoodsCatVO.class);
        return mallGoodsCatVOS;
    }

    //先查缓存，缓存中，用缓存的数据，无则用db
    // hash类型    portal:hotgoods     cat3id      MallGoodsAndCatVO.string
    @Override
    public List<MallGoodsAndCatVO> getAllGoodsAndCat() {
        BoundHashOperations<String, String, String> hotgoods = template.boundHashOps("portal:hotgoods");
        List<String> goods = hotgoods.values();
        if(goods==null || goods.size()==0){
            List<MallGoodsAndCatVO> allGoodsAndCat = goodsApi.getAllGoodsAndCat();
             for(MallGoodsAndCatVO mallGoodsAndCatVO:allGoodsAndCat){
                 hotgoods.put(mallGoodsAndCatVO.getCat3().getId()+"",JSONUtil.toJsonStr(mallGoodsAndCatVO));
             }
            return allGoodsAndCat;
        }
        List<MallGoodsAndCatVO> list = new ArrayList<>();
        for(String s: goods){
            MallGoodsAndCatVO mallGoodsAndCatVO = JSONUtil.toBean(s, MallGoodsAndCatVO.class);
            list.add(mallGoodsAndCatVO);
        }
        return list;
    }
}
